/***
 * @author gaotang
 * @date 2018-07-26
 */

import crypto from "crypto";
import { TOKENS } from "./Setting";

export default {
  algorithm: {
    ecb: 'des-ecb',
    cbc: 'des-cbc'
  },
  encrypt(plaintext, iv) {
    let cipher = crypto.createCipheriv(this.algorithm.ecb, new Buffer(TOKENS.API.TEST.KEY), new Buffer(iv ? iv : 0));

    cipher.setAutoPadding(true) //default true
    let ciph = cipher.update(plaintext, 'utf8', 'base64');
    ciph += cipher.final('base64');
    return ciph;
  },
  decrypt(encrypt_text, iv) {
    let decipher = crypto.createDecipheriv(this.algorithm.ecb, new Buffer(TOKENS.API.TEST.KEY), new Buffer(iv ? iv : 0));
    decipher.setAutoPadding(true);
    let txt = decipher.update(encrypt_text, 'base64', 'utf8');
    txt += decipher.final('utf8');
    return txt;
  }
}
